import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.io.File;
import java.io.StringWriter;
/**
 * Created by Administrator on 2016/10/9.
 */
class  student{
    public String information;
    public double score;
    student(String information,double score){
        this.information=information;
        this.score=score;
    }
     public  boolean find(double score){
        if(this.score==score){
            return  true;
        }
        else {
            return  false;
        }
    }
}
public class ReadTxtFile {
    public  static void getdata(String[] s,String filePath) {
        File file2 = new File(filePath);
        try {
            if (file2.isFile() && file2.exists()) {
                InputStreamReader isr2 = new InputStreamReader(new FileInputStream(file2), "UTF-8");
                BufferedReader br2 = new BufferedReader(isr2);
                int a = 0;
                while ((s[a] = br2.readLine()) != null) {
                    a++;
                }
                br2.close();
            } else {
                System.out.println("文件不存在");
            }
        } catch (Exception e) {

        }
    }
    public static  void main(String[] args){
        String filePath1="G:\\java\\exercise2\\src\\list.txt";
        String filePath2="G:\\java\\exercise2\\src\\studentname.txt";
        File file1=new File(filePath1);
        try {
           if (file1.isFile() && file1.exists()) {
                InputStreamReader isr1 = new InputStreamReader(new FileInputStream(file1), "UTF-8");
                BufferedReader br1 = new BufferedReader(isr1);
                String lineTxt1 =null;
                while ((lineTxt1 = br1.readLine()) != null) {
                    System.out.println(lineTxt1);
                }
                br1.close();
            } else {
                System.out.println("文件不存在");
            }
        }
         catch(Exception e){
        System.out.println("文件读取错误");
         }
        String[] lineTxt2 =new String[5];
        getdata(lineTxt2,filePath2);

        FileWriter writer;


        try{
            writer=new  FileWriter("src\\list.txt",true);
            for(int m=-1;m<5;m++){
                if(m==-1){
                    writer.write("\r");
                }
                else{
                    writer.write(lineTxt2[m]+"\r");
                }
            }
            writer.flush();
            writer.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        double[][] num=new double[5][5];
        double[] allclass=new double[5];
        Random s=new Random();
        for(int i=0;i<5;i++) {
            allclass[i]=0;
            for (int j = 0; j < 5; j++) {
                num[i][j] = 25 * s.nextGaussian() + 75;
                allclass[i]=allclass[i]+num[i][j];
            }
        }//0对应张三，1对应李四，2对应王五，3对应赵六，4对应孟伟
        Writer writer1;
        try {
            writer1=new FileWriter("src\\list.txt",true);
            writer1.write("\r");
            for(int p=0;p<5;p++){
                for(int q=0;q<5;q++){
                    if(q==4){
                        writer1.write(" "+num[p][q]+"\r");
                    }
                    writer1.write(" "+num[p][q]+" ");
                }
            }
            writer1.flush();
            writer1.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        List<student> students=new ArrayList<student>() ;
        for(int a=0;a<5;a++){
            student newstudent=new student(lineTxt2[a],allclass[a]);
            students.add(newstudent);
        }
        double[]  compare=allclass;
        double temp=0;
        for(int b=0;b<5;b++){
            for(int c=b+1;c<5;c++){
                if(compare[b]>compare[c]){
                    temp=compare[b];
                    compare[b]=compare[c];
                    compare[c]=temp;
                }
            }
        }
        Writer writer2;
        try {
            writer2 = new FileWriter("src\\list2.txt", true);
            writer2.write("\r");
            for (int p = 0; p < 5; p++) {
                for (int q = 0; q < 5; q++) {
                    if (students.get(q).find(compare[p])) {
                        writer2.write(students.get(q).information + "\r");
                    }
                }
            }
                writer2.flush();
                writer2.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }

        double avechinese=(double)((num[0][1]+num[0][1]+num[0][2]+num[0][3]+num[0][4])/5);
        double avemath=(double)((num[1][1]+num[1][1]+num[1][2]+num[1][3]+num[1][4])/5);
        double aveenglish=(double)((num[2][1]+num[2][1]+num[2][2]+num[2][3]+num[2][4])/5);
        double avephysics=(double)((num[3][1]+num[3][1]+num[3][2]+num[3][3]+num[3][4])/5);
        double avechemistry=(double)((num[4][1]+num[4][1]+num[4][2]+num[4][3]+num[4][4])/5);
        System.out.println("语文平均分为"+avechinese);
        System.out.println("数学平均分为"+avemath);
        System.out.println("英语平均分为"+aveenglish);
        System.out.println("物理平均分为"+avephysics);
        System.out.println("化学平均为为"+avechemistry);
    }
    }

